package com.whnk.pm.service.impl;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

import cn.hutool.core.exceptions.ValidateException;
import com.whnk.common.exception.DemoModeException;
import com.whnk.common.utils.StringUtils;
import com.whnk.pm.domain.EduInfo;
import com.whnk.pm.dto.Duration;
import com.whnk.pm.mapper.EduInfoMapper;
import com.whnk.pm.service.IPmCommonService;
import com.whnk.pm.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.whnk.pm.mapper.ProjInfoMapper;
import com.whnk.pm.domain.ProjInfo;
import com.whnk.pm.service.IProjInfoService;
import com.whnk.common.support.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 项目经历 服务层实现
 * 
 * @author whwhnk
 * @date 2019-06-13
 */
@Service
public class ProjInfoServiceImpl implements IProjInfoService
{
	@Autowired
	private ProjInfoMapper projInfoMapper;

	@Autowired
	private IPmCommonService iPmCommonService;

	/**
     * 查询项目经历信息
     * 
     * @param projId 项目经历ID
     * @return 项目经历信息
     */
    @Override
	public ProjInfo selectProjInfoById(Integer projId)
	{
	    return projInfoMapper.selectProjInfoById(projId);
	}
	
	/**
     * 查询项目经历列表
     * 
     * @param projInfo 项目经历信息
     * @return 项目经历集合
     */
	@Override
	public List<ProjInfo> selectProjInfoList(ProjInfo projInfo)
	{
		List<ProjInfo> projInfoList = projInfoMapper.selectProjInfoList(projInfo);
		projInfoList.sort(Comparator.comparing((k1) -> (
				(k1.getEmpPjEddt() == null ? new Date(): k1.getEmpPjEddt())
		)));
		return projInfoList;
	}
	
    /**
     * 新增项目经历
     * 
     * @param projInfo 项目经历信息
     * @return 结果
     */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public int insertProjInfo(ProjInfo projInfo)
	{
		iPmCommonService.eduAndWordCheck(projInfo.getEmpId(), projInfo.getEmpPjStdt());
		iPmCommonService.workProjDateCheck(projInfo);
	    int result = projInfoMapper.insertProjInfo(projInfo);
		staticProject(projInfo.getEmpId());
	    return result;
	}
	
	/**
     * 修改项目经历
     * 
     * @param projInfo 项目经历信息
     * @return 结果
     */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public int updateProjInfo(ProjInfo projInfo)
	{
		iPmCommonService.eduAndWordCheck(projInfo.getEmpId(), projInfo.getEmpPjStdt());
		iPmCommonService.workProjDateCheck(projInfo);
//		int result = projInfoMapper.updateProjInfo(projInfo);
		int result = projInfoMapper.insertProjInfo(projInfo);
		staticProject(projInfo.getEmpId());
	    return result;
	}

	/**
     * 删除项目经历对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public int deleteProjInfoByIds(String ids)
	{
		String[] strings = Convert.toStrArray(ids);
		String empId = projInfoMapper.selectProjInfoById(Integer.parseInt(strings[0])).getEmpId();
		int result = projInfoMapper.deleteProjInfoByIds(strings);
		staticProject(empId);
		return result;
	}


	/**
	 * 验证时间 不可重复
	 * @return
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public String checkDate(String stDt, String endDt, String empid, String operationType, String id) throws ParseException {
		DateFormat dateTimeFormatter = new SimpleDateFormat("yyyy-MM");
		Date startDate = dateTimeFormatter.parse(stDt);
		if (StringUtils.isBlank(endDt)) {
			endDt = dateTimeFormatter.format(new Date());
		}
		Date endDate = dateTimeFormatter.parse(endDt);
		if (endDate.before(startDate)) {
			throw new RuntimeException();
		}
		if (StringUtils.equalsIgnoreCase("edit", operationType)) {
			projInfoMapper.deleteProjInfoById(Integer.parseInt(id));
		}
		ProjInfo projInfo = new ProjInfo();
		projInfo.setEmpId(empid);
		List<Duration> list =
				projInfoMapper.selectProjInfoList(projInfo).stream().map(k ->
						new Duration(k.getEmpPjStdt(), k.getEmpPjEddt())
				).collect(Collectors.toList());
		return DateUtil.durationOverlap(startDate, endDate, list, operationType);
	}

	@Override
	public void staticProject(String empId) {
		ProjInfo proj = new ProjInfo();
		proj.setEmpId(empId);
		List<ProjInfo> projInfoList = projInfoMapper.selectProjInfoList(proj);
		Map<String, List<ProjInfo>> map = new HashMap<>();
		map.put(empId, projInfoList);
		iPmCommonService.staticBankYears(map);
		iPmCommonService.staticCcbYears(map);
	}

	@Override
	public void lastOneCheck(String empId, Integer projId) {
		ProjInfo projInfo = new ProjInfo();
		projInfo.setEmpId(empId);
		List<ProjInfo> projInfoList = projInfoMapper.selectProjInfoList(projInfo);
		Integer LastProjId = projInfoList.get(projInfoList.size() - 1).getProjId();
		if (!org.apache.commons.lang3.StringUtils.equalsIgnoreCase(String.valueOf(LastProjId).trim(), String.valueOf(projId).trim())) {
			throw new ValidateException("只允许修改最后一条记录的项目时间");
		}
		if (projInfoList.get(projInfoList.size() - 1).getEmpPjEddt() != null) {
			throw new ValidateException("项目结束时间不为空，不可修改");
		}
	}
}
